<?php

class profileModel extends Model{
    public function selectProfile($id){
        $sql = "select 
h.id,h.name,h.big_name,h.img,h.big_img,h.live,h.physics,h.magic,h.difficulty,
t.type,
p.name as pname,p.passive_img as pimg,p.describe as pd,
q.name as qname,q.q_img as qimg,q.describe as qd,
w.name as wname,w.w_img as wimg,w.describe as wd,
e.name as ename,e.e_img as eimg,e.describe as ed,
r.name as rname,r.r_img as rimg,r.describe as rd
from hero h 
left join herotype t on h.type = t.id 
left join h_passive p on p.hid = h.id 
left join h_q q on q.hid = h.id
left join h_w w on w.hid = h.id
left join h_e e on e.hid = h.id
left join h_r r on r.hid = h.id
where h.id = $id";
        return $this->link->selectByAll($sql);

    }
    public function selectType(){
        $sql = "select * from herotype";
        return $this->link->selectByAll($sql);
    }
    private function updateImg($img){
        $fileName = $img["name"];
        $upload_path = "./public/img/";
        $newname = substr_replace($fileName, date("YmdHis") . rand(100, 999), 0, strrchr(".", $fileName));
        $url = $upload_path . $newname;
        if (move_uploaded_file($img["tmp_name"], $url)) {
            return $url;
        }
    }
    public function modifyHeroInfo($id,$post,$file){
        $name = $post['name'];
        $big_name = $post['big_name'];
        $img = $this->updateImg($file['img0']);
        $imgField = '';
        if($img){
             $imgField = "img = '$img',";
        }
        $big_img = $this->updateImg($file['img1']);
        $big_imgField = '';
        if($big_img){
             $big_imgField = "big_img = '$big_img',";
        }
        $live = $post['live'];
        $physics = $post['physics'];
        $magic = $post['magic'];
        $difficulty = $post['difficulty'];
        $type = $post['type'];
        $sql = "update hero set 
        name = '$name',
        big_name = '$big_name',
        $imgField
        $big_imgField
        live = $live,
        physics = $physics,
        magic = $magic,
        difficulty = $difficulty,
        type = $type
        where id = $id
        ";
        $hero = $this->link->update($sql);

        $pname = $post['pname'];
        $pimg = $this->updateImg($file['img2']);
        $pimgField = '';
        if($pimg){
             $pimgField = "passive_img = '$pimg',";
        }
        $pd = $post['pd'];
        $sql = "update h_passive set 
        name = '$pname',
        $pimgField
        `describe` = '$pd'
        where hid = $id
        ";
        $p = $this->link->update($sql);

        $qname = $post['qname'];
        $qimg = $this->updateImg($file['img3']);
        $qimgField = '';
        if($qimg){
             $qimgField = "q_img = '$qimg',";
        }
        $qd = $post['qd'];
        $sql = "update h_q set 
        name = '$qname',
        $qimgField
        `describe` = '$qd'
        where hid = $id
        ";
        $q = $this->link->update($sql);

        $wname = $post['wname'];
        $wimg = $this->updateImg($file['img4']);
        $wimgField = '';
        if($wimg){
             $wimgField = "w_img = '$wimg',";
        }
        $wd = $post['wd'];
        $sql = "update h_w set 
        name = '$wname',
        $wimgField
        `describe` = '$wd'
        where hid = $id
        ";
        $w = $this->link->update($sql);
        
        $ename = $post['ename'];
        $eimg = $this->updateImg($file['img5']);
        $eimgField = '';
        if($eimg){
             $eimgField = "e_img = '$eimg',";
        }
        $ed = $post['ed'];
        $sql = "update h_e set 
        name = '$ename',
        $eimgField
        `describe` = '$ed'
        where hid = $id
        ";
        $e = $this->link->update($sql);

        $rname = $post['rname'];
        $rimg = $this->updateImg($file['img6']);
        $rimgField = '';
        if($rimg){
             $rimgField = "r_img = '$rimg',";
        }
        $rd = $post['rd'];
        $sql = "update h_r set 
        name = '$rname',
        $rimgField
        `describe` = '$rd'
        where hid = $id
        ";
        $r = $this->link->update($sql);
        
        if($hero&&$p&&$q&&$w&&$e&&$r){
            return true;
        }else{
            return false;
        }
    }
}